import requests
from requests.adapters import HTTPAdapter
from requests.adapters import Retry
# # 代理服务器的地址和端口
# proxy_url = 'http://dyf:Adsads123@127.0.0.1:18889'
# proxy_url_https = 'http://dyf:Adsads123@127.0.0.1:18889'
#
# # 配置代理
# proxies = {
#     'http': proxy_url,
#     'https': proxy_url_https,
# }
# # 使用代理发送请求
# response = requests.get(url='https://www.bbs.com/', proxies=proxies,verify=False,timeout=10)
#
# # 打印响应内容
# print(response.text)

class HttpClient(object):
    def __init__(self):
        # 代理服务器的地址和端口
        self.proxy_url = 'http://dyf:Adsads123@127.0.0.1:18889'
        self.proxy_url_https = 'http://dyf:Adsads123@127.0.0.1:18889'

        # 配置代理
        self.proxies = {
            'http': self.proxy_url,
            'https': self.proxy_url_https,
        }
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36'}

        self.session = requests.Session()
        self.retries = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])
        self.session.mount('http://', HTTPAdapter(max_retries=self.retries))
        self.session.mount('https://', HTTPAdapter(max_retries=self.retries))

    def download_url_main(self,input_url):
        # https://news.yahoo.com/ https://www.bss.com
        # print(proxies)
        # {'timeout': 30, 'allow_redirects': True, 'proxies': OrderedDict([('http', 'http://dyf:Adsads123@127.0.0.1:18889/'), ('https', 'https://dyf:Adsads123@127.0.0.1:18889/')]), 'stream': False, 'verify': False, 'cert': None}
        response = self.session.get(input_url,headers=self.headers, proxies=self.proxies, timeout=30,verify=False)
        #
        # # 目标网址
        # url = 'https://www.bbc.com/'
        #
        # # 发送GET请求
        # response = requests.get(url,timeout=10)
        #
        # 检查请求是否成功
        filename = input_url.replace("https://","").replace("/","")+".html"
        if response.status_code == 200:
            # 获取网页内容
            data = response.text
            # 指定文件名保存内容
            filename = filename
            with open(filename, 'w', encoding='utf-8') as file:
                file.write(data)
            print(f'网页数据已保存到 {filename}')
        else:
            print('请求失败')
        return filename